import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:situation/app/router/router_name.dart';
import 'package:situation/data/database/sql_provider.dart';
import 'package:situation/data/store/server_url_store.dart';
import 'package:situation/domain/common/server_config.dart';
import 'package:situation/domain/db/system_config_domain.dart';
import 'package:situation/shared/constant/app_constant.dart';
import 'package:situation/shared/util/util_sp.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

import 'state.dart';

class ServerUrlSettingController extends GetxController {
  final ServerUrlSettingState state = ServerUrlSettingState();

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    initData();
  }

  initData() async {
    var d = await SqlProvider.instance.query(SystemConfigDomain.tableName);
    state.serverList =
        d
            .map<SystemConfigDomain>(
              (item) => SystemConfigDomain.fromJson(item),
            )
            .toList();
  }

  add() async {
    var result = await Get.toNamed(
      RouterName.serverUrlForm,
      arguments: SystemConfigDomain(name: "", serverUrl: "", intercomUrl: ""),
    );
    if (result) {
      initData();
    }
  }

  void select(i) {
    showGeneralDialog(
      context: Get.context!,
      pageBuilder: (
        BuildContext buildContext,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
      ) {
        return TDAlertDialog(
          title: "确认切换么？切换后需重启APP",
          rightBtnAction: () {
            // 确认切换
            updateSystem(i);
            Get.back();

            // @todo   自动重启APP
          },
        );
      },
    );
  }

  void updateSystem(i) async {
    UtilSp.putInt(AppConstant.systemConfigIndex, i);
    UtilSp.putString(
      AppConstant.systemConfigIntercomUrl,
      state.serverList[i].intercomUrl,
    );

    UtilSp.putString(
      AppConstant.systemConfigServerUrl,
      state.serverList[i].serverUrl,
    );
    state.serverList = state.serverList.toList();
  }

  void edit(i) async {
    var result = await Get.toNamed(
      RouterName.serverUrlForm,
      arguments: state.serverList[i],
    );

    if (result) {
      await initData();
      if (UtilSp.getInt(AppConstant.systemConfigIndex)! == i) {
        updateSystem(i);
      }
    }
  }

  void remove(i) async {
    showGeneralDialog(
      context: Get.context!,
      pageBuilder: (
        BuildContext buildContext,
        Animation<double> animation,
        Animation<double> secondaryAnimation,
      ) {
        return TDAlertDialog(
          title: "确认删除么？此操作不可逆",
          rightBtnAction: () async {
            // 确认切换
            await SqlProvider.instance.delete(
              SystemConfigDomain.tableName,
              " id = ?",
              [state.serverList[i].id],
            );
            state.serverList = state.serverList.toList();
            await initData();

            if (UtilSp.getInt(AppConstant.systemConfigIndex)! == i) {
              updateSystem(0);
              UtilSp.putInt(AppConstant.systemConfigIndex, 0);
            }

            Get.back();
          },
        );
      },
    );
  }
}
